/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *   g.latarowski@gmail.com                                                *
 *   maciej.gajewski0@gmail.com                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// Qt
#include <QImageIOHandler>

// local
#include "gjpeg2khandler.h"


// ---------------------------------------------------------------------------
/*!
 * Image I/O plugin for a JPEG 2000 compressed file format.
 *
 * It uses OpenJPEG library for image reading/writing.
 *
 * No writing support yet.
 * TODO: writing support.
 */
// ---------------------------------------------------------------------------
class GJpeg2kPlugin : public QImageIOPlugin
{
public:
        /*!
        * Construct the plugin.
        */
    GJpeg2kPlugin();
        /*!
        * Plugin destructor.
        */
    ~GJpeg2kPlugin();

        /*!
        * Returns a list of the format string recognized by the plugin.
        * Currently return only "jp2" string.
        */
    QStringList keys() const;
        /*!
        * Determines the read/write capabilities of the plugin
        *
        * or rather, of the handler it provides, based on a given
        * I/O device or format string. When a format string is not given,
        * the plugin calls the static function canRead() of GJpeg2kHandler
        * to determine if the device's contents indicate the presence of
        * a JPEG 2000 image.
        *
        * @param device device on witch plugin determines read/write capabilities
        * @param format format string on witch plugin can determine
        *
        * @return read/write capabilities
        */
    Capabilities capabilities( QIODevice * pDevice,
                               const QByteArray & format
							 ) const;
        /*!
        * Creates an instance of the image I/O handler itself.
        *
        * @param device the device to handle with
        * @param format a specific file format
        *
        * @return JPEG 2000 image I/O handler
        */
    QImageIOHandler * create( QIODevice * pDevice,
							  const QByteArray & format = QByteArray()
							) const;
    
}; // class !GJpeg2kPlugin{}
// ---------------------------------------------------------------------------


// ---------------------------- GJpeg2kPlugin() ------------------------------
GJpeg2kPlugin::GJpeg2kPlugin()
{
	// none
}
// ---------------------------------------------------------------------------


// ---------------------------- ~GJpeg2kPlugin() -----------------------------
GJpeg2kPlugin::~GJpeg2kPlugin()
{
	// none
}
// ---------------------------------------------------------------------------


// --------------------------- keys() ----------------------------------------
QStringList GJpeg2kPlugin::keys() const
{
        // return supported format strings 
    return QStringList() << "jp2" << "j2c";
}
// ---------------------------------------------------------------------------


// ----------------------------- capabilities() ------------------------------
QImageIOPlugin::Capabilities GJpeg2kPlugin::capabilities( QIODevice * pDevice,
		const QByteArray & format ) const
{
		// Determine read/write capabilities based on format string 
	if( format == "jp2" || format == "j2c")
	{
			// correct file format
			// return read && write capabilities 
		return Capabilities( CanRead );
	}
	if( !( format.isEmpty() && pDevice->isOpen() ) )
	{
			// incorrect file format
			// return capabilities that says: no read and write
		return 0;
	}
	
		// format string is empty, determine read/write capabilities
		// based on device state 
	Capabilities cap;
	if( pDevice->isReadable() && GJpeg2kHandler::canRead( pDevice ) )
	{
			// device is readable and the correct magic was found
			// extend capabilities for reading 
		cap |= CanRead;
	}
	return cap;
}
// ---------------------------------------------------------------------------


// --------------------------- create() --------------------------------------
QImageIOHandler *GJpeg2kPlugin::create( QIODevice * pDevice,
										const QByteArray & format
									  ) const
{
		// create JPEG 2000 handler 
	QImageIOHandler * pHandler = new GJpeg2kHandler;
		// set its properties 
	pHandler->setDevice( pDevice );
	pHandler->setFormat( format );
		// return created handler
	return pHandler;
}
// ---------------------------------------------------------------------------


// ---------------------------------------------------------------------------
// export plugin macros for Qt detection
Q_EXPORT_STATIC_PLUGIN( GJpeg2kPlugin );
Q_EXPORT_PLUGIN2( gjpeg2kj2kcodec, GJpeg2kPlugin );


// eof
